/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Manpreet
 */
public class Frame extends JFrame {

    private Display display;
    private PanelButtons buttons;
    private JButton start;
    private JButton stop;
    private JButton CLaneX;
    private JButton CLaneY;
    private JButton CProbX;
    private JButton CProbY;
    private JButton Down;
    private JButton Across;
    private int xlanes;
    private String x;
    private int ylanes;
    private String y;
    private double xprob;
    private double yprob;

    /**
     * This class Creates a frame and adds predetermined panels to the North and South
     * (Class PanelButtons) the south panel has a grid layout of 4 rows and 2 columns so that the menu buttons can be added
     * (Class Display) the north panel is added for the display of the simulation
     *
     */
    public Frame() {
        this.setResizable(false);
        setTitle("Traffic Simulator");

        display = new Display();

        buttons = new PanelButtons();

        start = new JButton("Start");
        stop = new JButton("Stop");
        CLaneX = new JButton("Change No. of X lanes");
        CLaneY = new JButton("Change No. of Y lanes");
        CProbX = new JButton("Change Probability of X lane");
        CProbY = new JButton("Change Probability of Y lane");
        Down = new JButton("Traffic Down");
        Across = new JButton("Traffic Across");

        buttons.add(start);
        buttons.add(stop);
        buttons.add(Down);
        buttons.add(Across);
        buttons.add(CLaneX);
        buttons.add(CLaneY);
        buttons.add(CProbX);
        buttons.add(CProbY);

        add(display, BorderLayout.NORTH);
        add(buttons, BorderLayout.SOUTH);

        start.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                display.start();
            }
        });

        stop.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                display.stop();
            }
        });

        Down.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                display.setLightDown();
            }
        });

        Across.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                display.setLightAcross();
            }
        });

        CLaneX.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                boolean r = true;
                while (r == true) {
                    r = false;
                    try {
                        x = JOptionPane.showInputDialog("Enter the amount of X Car lanes (1-3)");
                        if (x == null) {
                            break;
                        }
                        xlanes = Integer.parseInt(x);


                        if (xlanes < 1 || xlanes > 3) {
                            r = true;
                        } else {
                            display.setXlane(xlanes);
                        }
                    } catch (Exception i) {
                        r = true;
                    }
                }

            }
        });

        CLaneY.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                boolean r = true;
                while (r == true) {
                    r = false;
                    try {
                        y = JOptionPane.showInputDialog("Enter the amount of Y Car lanes (1-4)");
                        if (y == null) {
                            break;
                        }
                        ylanes = Integer.parseInt(y);
                        if (ylanes < 1 || ylanes > 4) {
                            r = true;
                        }else {
                            display.setYlane(ylanes);
                        }
                    } catch (Exception i) {
                        r = true;
                    }
                }                
            }
        });

        CProbX.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                boolean r = true;
                while (r == true) {
                    r = false;
                    try {
                        x = JOptionPane.showInputDialog("Enter Probability for X lane (0-1)");
                        if (x == null) {
                            break;
                        }
                        xprob = Double.parseDouble(x);
                        if (xprob < 0 || xprob > 1) {
                            r = true;
                        }else {
                            display.setXprob(xprob);
                        }

                    } catch (Exception i) {
                        r = true;
                    }
                }
                display.setXprob(xprob);
            }
        });

        CProbY.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                boolean r = true;
                while (r == true) {
                    r = false;
                    try {
                        y = JOptionPane.showInputDialog("Enter Probability for Y lane (0-1)");
                        if (y == null) {
                            break;
                        }
                        yprob = Double.parseDouble(y);
                        if (yprob < 0 || yprob > 1) {
                            r = true;
                        }else {
                            display.setYprob(yprob);
                        }
                    } catch (Exception i) {
                        r = true;
                    }
                }

            }
        });

        pack();
    }
}
